home *** CD-ROM | disk | FTP | other *** search
/ Freelog 45 / Freelog045.iso / Bas / Internet / VisitURL / setup.exe / {app} / perlre.txt < prev    next >
Text File  |  1998-12-15  |  6KB  |  166 lines

  1.  
  2.   TPerlRe - Luu Tran <luutran@geocities.com>
  3.  
  4.   Version 1.0
  5.  
  6.   Last Updated: 15-Dec-98
  7.  
  8.   This is the Delphi4 wrapper for perlre.dll, the win32 port of
  9.   Philip Hazel's PCRE (Perl Compatible Regular Expression) package.
  10.   PCRE itself can be found at:
  11.  
  12.   ftp://ftp.cus.cam.ac.uk/pub/software/programs/pcre/
  13.  
  14.   Read pcre.html.  This is the html-ized man page that came with pcre.
  15.  
  16.   This is freeware. The software is provided in hope that it will be 
  17.   of use.  The author is NOT responsible for any errors, mishaps, 
  18.   disasters, or calamities that may result from its use.
  19.  
  20.   Permission is granted to anyone to use this software for any purpose 
  21.   on any computer system, and to redistribute it freely, subject to 
  22.   the following restrictions:
  23.  
  24. 1. This software is distributed in the hope that it will be useful,
  25.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27.  
  28. 2. The origin of this software must not be misrepresented, either by
  29.    explicit claim or by omission.
  30.  
  31. 3. Altered versions must be plainly marked as such, and must not be
  32.    misrepresented as being the original software.
  33.  
  34.  
  35. ========================================================================
  36.  
  37. Turn on word wrap to better view this file.
  38.  
  39. Contents
  40.  
  41. 1) Using TPerlre
  42. 2) Known issues (IMPORTANT: please read).
  43. 3) Notes on building the dll.
  44.  
  45. 1) Using TPerlRe
  46.  
  47. TPerlRe is just a class wrapper to interface with perlre.dll.  I use Delphi4's overloading and default paramater quite a bit, so if you use an older version you'll have to make some modifications.  The interface is as follows:
  48.  
  49.     property RegExp: string
  50.         the regex pattern
  51.  
  52.     property Options: integer
  53.         ORed values of the following: see pcre.html for explanation of these
  54.         options
  55.           PCRE_CASELESS
  56.           PCRE_MULTILINE
  57.           PCRE_DOTALL
  58.           PCRE_SINGLELINE
  59.           PCRE_EXTENDED
  60.           PCRE_ANCHORED
  61.           PCRE_DOLLAR_ENDONLY
  62.           PCRE_EXTRA
  63.           PCRE_NOTBOL
  64.           PCRE_NOTEOL
  65.           PCRE_UNGREEDY
  66.  
  67.     property Text: string
  68.         subject text to match
  69.  
  70.     property SubExp[I:integer]: TSubExp
  71.         returns info for the Ith subexpression, where 0th expression
  72.         is the entire matching string
  73.         TSubExp stores 1) the subexpression string; 2) the 1-based index
  74.         of the subexpression into Text; and 3) length of subexpression.
  75.  
  76.     property SubExpCount: integer
  77.         returns how many sub expressions were matched (not incl. the 0th one)
  78.         
  79.     constructor Create( doStudy: boolean=true; opts:integer=DefaultOpts);
  80.         set doStudy to false if you don't want to study the regex,
  81.         i.e., optimize it.
  82.         (If you use the regex inside a loop, changes are you DO want to
  83.         study it.)
  84.  
  85.     procedure Reset;
  86.         TPerlre keeps an internal pointer of current position in the
  87.         Text, so that successive calls to Match will find more occurrences.
  88.  
  89.     procedure Split( const regexp: string; str: string; elems:
  90.                      TStrings; doTrim: boolean=true);
  91.         similar to Perl's split command
  92.         doTrim=true => trim leading and trailing space of split strings.
  93.  
  94.     procedure Compile( const regexp: string); overload;
  95.     procedure Compile( const regexp: string; MatchCase: boolean); overload;
  96.     procedure Compile( const regexp: string; opts: integer); overload;
  97.         different variations of Compile; not strictly necessary, just
  98.         makes life simpler for me.
  99.  
  100.     function Match: boolean; overload;
  101.     function Match( const str: string): boolean; overload;
  102.     function Match( const regexp, str: string): boolean; overload;
  103.     function Match( const regexp, str: string; opts: integer): boolean;
  104.                                                                overload;
  105.         Same here.  The last 3 matches function simply sets 
  106.         the parameters then call Match()
  107.  
  108. Here's a simple example of TPerlre at work:
  109.  
  110. function SaneEmail( const Str: string):boolean;
  111. begin
  112.   with TPerlre.Create( false, 0) do
  113.   Try
  114.     Result:=Match( '^[^@ ]+\@[^@ ]+\.[^@ ]+$', Str);
  115.   Finally
  116.     Free;
  117.   end;
  118. end;
  119.  
  120. ====================================================
  121.  
  122.  
  123. 2) Known issues.
  124.  
  125. First, I did not completely port some functions, notably those in maketables.c which allows you to define character tables for foreign languages and locales.
  126.  
  127. More importantly, I currently use a hack for freeing the (pcre *) and (pcre_extra *) pointers.  Since I cannot use Delphi's dispose to free the memory allocated by the dll, I introduced a function called pcre_dispose in the dll and  pass it the pointers to free (see next section).  This seems to work fine but i realize it's not the customary or preferred way.  If someone can improve this, please let me know.
  128.  
  129.  
  130. =====================================================
  131.  
  132.  
  133. 3) Notes on building the dll
  134.  
  135. I built the dll with MSVC++6.  It's actually pretty simple.  The first thing you need to do is compile the file deftables.c and run it.  This will generate the file chartables.c, which you'll need to build the dll.
  136.  
  137. Next, create an empty win32 dll.  Add the files pcre,c and study.c.  If you want the posix-type interface, then add pcreposix.c.  I didn't.
  138.  
  139. Next, you need to export some functions.  Edit pcre.h and add __declspec(dllexport) to all the function declarations.  For example:
  140.  
  141. change:
  142.  
  143. extern pcre *pcre_compile(const char *, int, 
  144.     const char **, int *, const unsigned char *);
  145.  
  146. to:
  147.  
  148. extern __declspec(dllexport) pcre *pcre_compile(const char *, int, 
  149.     const char **, int *, const unsigned char *);
  150.  
  151.  
  152. To make it work with Delphi, I also added a dispose function.  In pcre.h, declare it as:
  153.  
  154. extern __declspec(dllexport) void pcre_dispose(pcre *, pcre_extra *);
  155.  
  156. In pcre.c, define it as:
  157.  
  158. void pcre_dispose(pcre *p, pcre_extra *px)
  159. {
  160.     if (p!=NULL) free( p);
  161.     if (px!=NULL) free( px);
  162. }  
  163.  
  164.  
  165. That's it!  Select the build command.
  166.